//
//  WMEMySelfViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/27.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEMySelfViewController.h"
#import "WMELoginViewController.h"
#import "WMERegisterViewController.h"
#import "WMESetViewController.h"
#import "WMECollectViewController.h"
#import "UIView+hhhh.h"
#import <Masonry.h>
#import "WMEYiJianViewController.h"
#import "WMEReckonClearData.h"
#import "AppDelegate.h"

@interface WMEMySelfViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSArray *dataArr;
@property (nonatomic, retain)UIView *backView;
@property (nonatomic, retain)UISwitch *seitch;
@property (nonatomic, retain)UIView *headView;
@end

@implementation WMEMySelfViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_backView release];
    [_seitch release];
    [_headView release];
    [super dealloc];
}

#pragma mark - 视图将要出现
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
////    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
////    if ([user boolForKey:@"success"]) {
//        [self createHead];
//        [self loggedIn];
////    }else{
////        [self notLoggedIn];
////    }
//}

#pragma mark -视图已经加载
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = @[@"   我的收藏列表", @"   我有意见", @"   夜间模式", @"   "];
    [self createHead];
    [self loggedIn];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    self.backView  = [[ UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT)];
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.5;
    self.backView.userInteractionEnabled = NO;
    [delegate.window addSubview:_backView];
    _backView.hidden = YES;
    [_backView release];
    
    
    
    
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;

//    //需修改 修改界面的背景图片
//    self.view.backgroundColor = [UIColor whiteColor];
//    
//    //
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.frame = CGRectMake(WIDTH - 50, 30, 50, 30);
//    [btn setTitle:@"设置" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(shezheAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
//    
//    //登陆按钮 初始化 添加事件
//    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    loginButton.backgroundColor = [UIColor whiteColor];
//    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
//    [loginButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:loginButton];
//    
//    //注册按钮 初始化 添加事件
//    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
//    [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:registerButton];
//    
//    __block __typeof(&*self)bs = self;
//    
//    //注册按钮的位置设置
//    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 50));
//        make.centerY.mas_equalTo(loginButton.mas_centerY);
//        make.left.equalTo(bs.view).with.offset(50);
//        
//    }];
//    //登陆按钮的位置设置
//    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100,50));
//        make.centerY.mas_equalTo(self.view.mas_centerY);
//        make.right.equalTo(bs.view).with.offset(-50);
////        make.left.equalTo(registerButton.mas_right).with.offset(20);
////        make.left.equalTo(self).with.offset(100);
//    }];
    

}
#pragma mark - 创建头部视图
- (void)createHead
{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH)];
    [self.view addSubview:_headView];
    [_headView release];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:_headView.bounds];
    [_headView addSubview:img];
    img.image = [UIImage imageNamed:@"beijing"];
    [img release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 20, WIDTH / 2)];
    label.center = CGPointMake(_headView.center.x, _headView.center.y);
    [self.headView addSubview:label];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"人生若只如初见";
    label.numberOfLines = 0;
    label.font = [UIFont boldSystemFontOfSize:20];
    label.layer.cornerRadius = 15;
    label.clipsToBounds = YES;
    [label release];
    
//    UIImageView *userImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
//    [_headView addSubview:userImg];
//    userImg.backgroundColor = [UIColor whiteColor];
////    userImg.image = [UIImage imageNamed:@"touxiang"];
//    userImg.layer.cornerRadius = 40;
//    userImg.clipsToBounds = YES;
//    userImg.center = _headView.center;
//    [userImg release];
//    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, userImg.frame.size.height + userImg.frame.origin.y + 20, WIDTH, 20)];
//    [_headView addSubview:name];
//    //用户名从 本地 提取
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    name.text = [user objectForKey:@"userName"];
//    name.textAlignment = NSTextAlignmentCenter;
//    [name release];
    
}

#pragma mark -已经登陆
- (void)loggedIn
{
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    ////////////////自治导航栏//////////////////
    UIView *ttview = [[UIView alloc] init];
    //    ttview.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    ttview.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.view addSubview:ttview];
    //view布局
    [ttview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 64));
        make.centerX.mas_equalTo(self.view.center);
        make.top.equalTo(self.view).with.offset(0);
        make.left.equalTo (self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    [ttview release];
    //线
    UILabel *lll = [[UILabel alloc] init];
    //    __block WMEFindViewController *find = self;
    lll.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lll];
    [lll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 0.3));
        make.centerX.mas_equalTo(self.view.center);
        make.top.equalTo(self.view).with.offset(63);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    [lll release];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = _headView;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView release];
    
//    self.view.backgroundColor = [UIColor whiteColor];
//    //
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.frame = CGRectMake(WIDTH - 50, 30, 50, 30);
//    [btn setTitle:@"设置" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(shezheAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mySelf"];
    UITableViewCell *mySelf = [_tableView dequeueReusableCellWithIdentifier:@"mySelf" forIndexPath:indexPath];
    mySelf.textLabel.text = _dataArr[indexPath.row];
    
    if (indexPath.row == 2) {
        self.seitch = [[UISwitch alloc]init];
        mySelf.accessoryView = _seitch;
        
        [self.seitch addTarget:self action:@selector(swich:) forControlEvents:UIControlEventValueChanged];
        [_seitch release];
    }
    if (indexPath.row == 3) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        CGFloat h = [WMEReckonClearData folderSizeAtPath:path];
        mySelf.textLabel.text = [NSString stringWithFormat:@"   清除缓存       %.2fMB", h];
        mySelf.textLabel.textAlignment = NSTextAlignmentLeft;
        mySelf.tag = 10000;
        mySelf.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

//    mySelf.textLabel.textAlignment = NSTextAlignmentRight;
    return mySelf;
}

/**<  按钮方法中心 */
-(void)swich:(UISwitch *)button
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.on forKey:@"isNight"];
    if (button.on) {
        self.backView.hidden = NO;
    }else
    {
        self.backView.hidden = YES;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        WMECollectViewController *collec = [[WMECollectViewController alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:collec];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
        [collec release];
        [navi release];
    }else if(indexPath.row == 1){
        WMEYiJianViewController *yijian = [[WMEYiJianViewController alloc] init];
        UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:yijian];
        [self presentViewController:navi1 animated:YES completion:^{
            NSLog(@"模态进入意见界面");
        }];
        [navi1 release];
        [yijian release];
    }else if (indexPath.row == 3) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        // 提示控制器
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清空缓存么 亲" message:@"清除缓存会导致下次运行程序的时候拖慢 确认删除么" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击确认");
            [WMEReckonClearData clearCache:path];
            UITableViewCell *cell = [self.view viewWithTag:10000];
            CGFloat h = [WMEReckonClearData folderSizeAtPath:path];
            cell.textLabel.text = [NSString stringWithFormat:@"   清除缓存       %.2fMB", h];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
#pragma mark -未登陆
//- (void)notLoggedIn
//{
//    self.view.backgroundColor = [UIColor whiteColor];
//    UIImageView *bgImg = [[UIImageView alloc] initWithFrame:self.view.frame];
//    bgImg.image = [UIImage imageNamed:@"dl1"];
//    [self.view addSubview:bgImg];
//    [bgImg release];
//    //
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.frame = CGRectMake(WIDTH - 50, 30, 50, 30);
//    [btn setTitle:@"设置" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(shezheAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
//    
//    //登陆按钮 初始化 添加事件
//    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
////    loginButton.backgroundColor = [UIColor whiteColor];
//    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
//    [loginButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:loginButton];
//    
//    //注册按钮 初始化 添加事件
//    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
//    [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:registerButton];
//    
//    __block __typeof(&*self)bs = self;
//    
//    //注册按钮的位置设置
//    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 50));
//        make.centerY.mas_equalTo(loginButton.mas_centerY);
//        make.left.equalTo(bs.view).with.offset(50);
//        
//    }];
//    //登陆按钮的位置设置
//    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100,50));
//        make.centerY.mas_equalTo(self.view.mas_centerY);
//        make.right.equalTo(bs.view).with.offset(-50);
//        //        make.left.equalTo(registerButton.mas_right).with.offset(20);
//        //        make.left.equalTo(self).with.offset(100);
//    }];
//}
#pragma mark -登陆事件
//登陆按钮触发的事件 模态进入 登陆界面
//- (void)buttonAction:(UIButton *)button
//{
//    WMELoginViewController *loginVC = [[WMELoginViewController alloc] init];
//    [self presentViewController:loginVC animated:YES completion:^{
//        NSLog(@"模态进 登陆界面");
//    }];
//}

#pragma mark -注册事件
//注册按钮触发的事件 模态进入 注册界面
//- (void)registerButtonAction:(UIButton *)btn
//{
//    WMERegisterViewController *registerVC = [[WMERegisterViewController alloc] init];
//    [self presentViewController:registerVC animated:YES completion:^{
//        NSLog(@"模态进入 注册界面");
//    }];
//}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isNight = [defaults boolForKey:@"isNight"];
//    UISwitch *switchs = [self.view viewWithTag:999999];
    if (isNight) {
        _seitch.on = YES;
        //         [[NSNotificationCenter defaultCenter] postNotificationName:@"nightCome" object:nil userInfo:nil];
        
    }else{
        _seitch.on = NO;
        //        [[NSNotificationCenter defaultCenter] postNotificationName:@"dayCome" object:nil userInfo:nil];
    }
    
    
}
//- (void)shezheAction:(UIButton *)btn
//{
//    WMESetViewController *set = [[WMESetViewController alloc] init];
//    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:set];
//    [self presentViewController:navi animated:YES completion:^{
//    }];
//}
//

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
